var login_tabs;  
$(function() {
	//go to specified tab
	  var fullUrl=window.location.href;
		if (fullUrl.indexOf("#reg")!=-1) {
			login_tabs=$( "#reg-login-tabs" ).tabs({active:1}); //registration tab
		}else if (fullUrl.indexOf("#forgot")!=-1) {
			login_tabs=$( "#reg-login-tabs" ).tabs({active:2});//forgot your password tab
		}else {
			login_tabs=$( "#reg-login-tabs" ).tabs();//login tab
		}	    
    
    $( "form.edit input[type='text']" ).placeholder();
    $("form .dob").datepicker();
    
    //switch view/edit mode in my-profile page
    $("#registration input[type='radio'][name='mode']").click(function(){
    	if ($(this).val()=='view'){
    		$("form[name='dms-regirstration-form']").removeClass("edit");
    		$("form[name='dms-regirstration-form']").addClass("disabled");
    		$("form[name='dms-regirstration-form'] input,form[name='dms-regirstration-form'] select").prop("disabled",true);
    		$("form[name='dms-regirstration-form'] label.error").hide()
    	} else {
    		$("form[name='dms-regirstration-form']").removeClass("disabled");
    		$("form[name='dms-regirstration-form']").addClass("edit");
    		$("form[name='dms-regirstration-form'] label.error").show()
    		$("form[name='dms-regirstration-form'] input,form[name='dms-regirstration-form'] select").prop("disabled",false);
    	}
    })
  });
  //click reg/forgot link to active tab
  $("a[href*='#reg']").click(function(){
	  $( "#reg-login-tabs" ).tabs("option","active",1);
  })
  $("a[href*='#forgot']").click(function(){
	  $( "#reg-login-tabs" ).tabs("option","active",2);
  })
  $("a[href*='#log-in']").click(function(){
	  $( "#reg-login-tabs" ).tabs("option","active",0);
  })
  
  //add validator rule for date of birth, not allow future date
	$.validator.addMethod("dateNotInFuture", function(value, element) {
        var now = new Date();
        var valid=now.getTime() >= Date.parse(value) || value == "";
        return valid;
    }, "Date cannot be in future.");
	  
	$(".dms form[name='dms-login-form']").validate({
		messages:{
			userName:{
				required:"Missing user name.",
				email:"Invalid email address."
			},
			password:"Missing password."
		}
    });
	$(".dms form[name='forgot-password-form']").validate({
		messages:{
			userName:{
				required:"Missing user name.",
				email:"Invalid email address."
			}
		}
	});
	$(".dms form[name='dms-regirstration-form']").validate({
		messages:{
			firstName:"Missing First Name.",
			lastName:"Missing Last Name.",
			gender:"Missing Gender.",
			dateOfBirth:{
				required:"Missing Date of Birth."
			},
			language:"Select a language.",
			addressLine1:"Missing address.",
			city:"Missing city.",
			state:"Missing state.",
			zip:{
				required:"Missing zip code.",
				minlength:"minimum 5 digits",
				maxlength:"maximum 5 digits"
			},
			cellPhoneNumber:"Missing cell phone number.",
			type:"Missing user type.",
			userName:{
				required:"Missing user name.",
				email:"Invalid email address." ,
				remote:"User Name is already taken."
			},
			password:"Missing password.",
			password2:{
				required:"Missing confirmed password.",
				equalTo:"Password mismatch."
			}
		},
		// the errorPlacement has to take the table layout into account
		errorPlacement: function(error, element) {
			if ( element.is(":radio") )
				error.appendTo( element.parent().parent());
			else
				error.appendTo( element.parent() );
		}	
	});
